package admin.register;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class AddCourse
 */
public class AddCourse extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddCourse() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String scid = request.getParameter("schoolid");
		String courseid = request.getParameter("courseid");
		String coursename = request.getParameter("coursename");
		String description = request.getParameter("description");
		
		String query = "INSERT INTO courses (courseid, schoolid, coursename, description) values('"+courseid+"','"+scid+"','"+coursename+"','"+description+"')";
		System.out.println(query);
	    String connectionString = "jdbc:mysql://localhost:3306/yooim";
	    String userName = "root";
	    String password = "mapat";
	    Connection conn; 
	    ResultSet rs = null;
	    try {
	    	
	    	Class.forName("com.mysql.jdbc.Driver").newInstance();
	    	conn = DriverManager.getConnection(connectionString,userName,password);
	    	Statement st = conn.createStatement();
	    	st.execute(query);
	    	conn.close();
	    	response.sendRedirect("./index.html");
	    }
	    catch(Exception e)
	    {
	    	System.out.println("Error while connection to DB - addemail : "+e.toString());
	    	response.sendRedirect("./Index.html");
	    }
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
